Memory is a temporary storage area that you can use within the
functions to save the data by applying a special keyword “memory”
in front of a variable. It can’t be used to retain the data in between
the calls, and hence, the gas cost in storing the data in memory is far
less than saving the same in storage.
Refer to the following code:
// SPDX-License-Identifier: SOME IDENTIFIER
pragma solidity ^0.8.10;
contract StorageMemory {
function saveInMemory() public pure returns(string memory,
string memory){
string memory varStorage1 = “”;
string memory varStorage2 = “”;
return (varStorage1, varStorage2);
}
}
Please also note that the memory type of storage is applicable only
to the variables that pass by reference, i.e., Array, Stuct, String,
Mappings etc.
2.5.13.3 Calldata
Every variable of the type reference can be stored in either of the
three data locations, i.e., Storage, Memory, or Calldata. Calldata is a
non-modifiable, non-persistent area with almost a memory type of a
temporary storage, and therefore, only stores the function
arguments.
2.5.13.4 Stack
If the Solidity program needs to store small amount of local data
inside the functions to make sure of low usage of gas, then we have
to choose Stack. Stack is the data area for performing the
calculations and it can store a maximum of up to 1024 elements with
a maximum value of 256 bits.